package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.CheckGroupDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Administrator
 * @Date 2020/3/29 0029
 * @Version 1.0
 * @Description 检查组接口实现类
 */
@Transactional
@Service
public class CheckGroupServiceImpl implements CheckGroupService {
    //    依赖注入 调用dao中的分页查询
    @Autowired
    private CheckGroupDao checkGroupDao;

    @Override
    public PageResult pageQuery( QueryPageBean queryPageBean){
        //使用PageHelper工具实现设置分页，查询limit条件
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //        调用dao中的方法实现查询项数据
        Page<CheckGroup> page = checkGroupDao.selectCondition(queryPageBean.getQueryString());
        PageResult pageResult = new PageResult(page.getTotal(),page.getResult());
        System.out.println(pageResult);
        return pageResult;
    }

    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
//        System.out.println(checkGroup);
        checkGroupDao.add(checkGroup);
//遍历检查项
        for (Integer checkitemId : checkitemIds) {
            Map map = new HashMap();
            map.put("checkgroup_id",checkGroup.getId());
            map.put("checkitem_id",checkitemId);
            checkGroupDao.addCheckGroupAndCheckItem(map);
        }

    }

    @Override
    public CheckGroup findId(Integer id) {
        System.out.println(checkGroupDao.findId(id));
        return checkGroupDao.findId(id);
    }

    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
//        System.out.println(checkGroupDao.findCheckItemIdsByCheckGroupId(id));
        return checkGroupDao.findCheckItemIdsByCheckGroupId(id);
    }

    @Override
    public void update(CheckGroup checkGroup, Integer[] id) {
        //对原基本信息进行更新
        checkGroupDao.update(checkGroup);
        //删除 检查项的原数据
        checkGroupDao.deletecheckitemId(checkGroup.getId());
        //新增基本信息关联的检查项数据
        for (Integer checkitemId : id) {
            Map map = new HashMap();
            map.put("checkgroup_id",checkGroup.getId());
            map.put("checkitem_id",checkitemId);
            checkGroupDao.addCheckGroupAndCheckItem(map);
        }
    }

    @Override
    public void delete(Integer id) {
        checkGroupDao.delete(id);
    }

    @Override
    public List<CheckGroup> findAll() {
        return checkGroupDao.findAll();
    }


}
